package cst.a729yt.zyfypt_729yt.Activity;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import cst.a729yt.zyfypt_729yt.Bean.VideoBean;
import cst.a729yt.zyfypt_729yt.Fragment.ArticleFragment;
import cst.a729yt.zyfypt_729yt.Fragment.IFragment;
import cst.a729yt.zyfypt_729yt.Fragment.TcaseFragment;
import cst.a729yt.zyfypt_729yt.Fragment.TwareFragment;
import cst.a729yt.zyfypt_729yt.Fragment.VideoFragment;
import cst.a729yt.zyfypt_729yt.R;
import cst.a729yt.zyfypt_729yt.ResModel;
import cst.a729yt.zyfypt_729yt.ytListener;

public class MainActivity extends AppCompatActivity {
    private ResModel model;
    private ytListener<List<VideoBean>> listener;

    private LinearLayout  layout1,layout2,layout3,layout4,layout5;//用于界面切换
    private ImageButton ib1,ib2,ib3,ib4,ib5;//用户设置图片
    private ViewPager vp;

    private List<Fragment> listfragment;
    private FragmentPagerAdapter fpadapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        init();
        initEvents();
        initFragment();
        model = new ResModel("gson");
        listener = new ytListener<List<VideoBean>>() {
            @Override
            public void onResponse(List<VideoBean> videoBeen) {

                String s = "test";
            }

            @Override
            public void onFail(String msg) {

                System.out.print(msg);
            }
        };

        model.getList("video", 1, listener);
    }
    private void initFragment() {
        listfragment=new ArrayList<Fragment>();
        listfragment.add(new ArticleFragment());
        listfragment.add(new TwareFragment());
        listfragment.add(new VideoFragment());
        listfragment.add(new TcaseFragment());
        listfragment.add(new IFragment());


        fpadapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return listfragment.get(position);
            }

            @Override
            public int getCount() {
                return listfragment.size();
            }
        };
        vp.setAdapter(fpadapter);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                System.out.println("----"+String.valueOf(position));
                vp.setCurrentItem(position);
                resetImage();
                selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    View.OnClickListener  onClickListener =new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //先将四个按钮都设成未选图片
            resetImage();
            switch (view.getId())
            {
                case R.id.layout1:
                    //Toast.makeText(MainActivity.this, "博文", Toast.LENGTH_SHORT).show();
                    selectTab(0);
                    break;
                case R.id.layout2:
                    //Toast.makeText(MainActivity.this, "课件", Toast.LENGTH_SHORT).show();
                    selectTab(1);
                    break;
                case R.id.layout3:
                    //Toast.makeText(MainActivity.this, "视频", Toast.LENGTH_SHORT).show();
                    selectTab(2);
                    break;
                case R.id.layout4:
                    //Toast.makeText(MainActivity.this, "案例", Toast.LENGTH_SHORT).show();
                    selectTab(3);
                    break;
                case R.id.layout5:
                    //Toast.makeText(MainActivity.this, "我", Toast.LENGTH_SHORT).show();
                    selectTab(4);
                    break;
            }
        }
    };
    private void initEvents() {
        layout1.setOnClickListener(onClickListener);
        layout2.setOnClickListener(onClickListener);
        layout3.setOnClickListener(onClickListener);
        layout4.setOnClickListener(onClickListener);
        layout5.setOnClickListener(onClickListener);
    }

    private void init() {
        layout1=(LinearLayout) findViewById(R.id.layout1);
        layout2=(LinearLayout)findViewById(R.id.layout2);
        layout3=(LinearLayout)findViewById(R.id.layout3);
        layout4=(LinearLayout)findViewById(R.id.layout4);
        layout5=(LinearLayout)findViewById(R.id.layout5);

        ib1=(ImageButton)findViewById(R.id.imageButton1);
        ib2=(ImageButton)findViewById(R.id.imageButton2);
        ib3=(ImageButton)findViewById(R.id.imageButton3);
        ib4=(ImageButton)findViewById(R.id.imageButton4);
        ib5=(ImageButton)findViewById(R.id.imageButton5);

        vp=(ViewPager)findViewById(R.id.viewPager);
    }

    private  void resetImage()
    {
        ib1.setImageResource(R.drawable.article_pressed);
        ib2.setImageResource(R.drawable.tware_normal);
        ib3.setImageResource(R.drawable.video_normal);
        ib4.setImageResource(R.drawable.tcase_normal);
        ib5.setImageResource(R.drawable.me_normal);
    }

    private void selectTab(int i)
    {
        switch (i)
        {
            case 0:
                ib1.setImageResource(R.drawable.article_pressed);
                break;
            case 1:
                ib2.setImageResource(R.drawable.tware_pressed);
                break;
            case 2:
                ib3.setImageResource(R.drawable.video_pressed);
                break;
            case 3:
                ib4.setImageResource(R.drawable.tcase_pressed);
                break;
            case 4:
                ib5.setImageResource(R.drawable.me_pressed);
                break;
        }
        vp.setCurrentItem(i);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}
